<template>
    <div>
      <!-- 面包屑导航条 -->
      <el-breadcrumb separator-class="el-icon-arrow-right">
        <el-breadcrumb-item :to="{ path: '/index' }">首页</el-breadcrumb-item>
        <el-breadcrumb-item>数据中心</el-breadcrumb-item>
        <el-breadcrumb-item>原材料管理</el-breadcrumb-item>
      </el-breadcrumb>
      <el-card class="box-card">
        <el-form :model="material" class="demo-form-inline" >
          <el-row :gutter="20">
            <el-form-item>
              <el-col :span="4">
                <el-input placeholder="材料名称" v-model="material.name"></el-input>
              </el-col>
              <el-col :span="4">
                <el-input placeholder="化学元素名称" v-model="material.chemical"></el-input>
              </el-col>
              <el-button plain type="info"@click="sel">搜索</el-button>
              <el-button  type="primary" @click="toAdd">添加原材料</el-button>
            </el-form-item>
          </el-row>
        </el-form>
        <el-table
          :data="materialList"
          style="width: 100%">
          <el-table-column
            prop="code"
            label="编号"
            width="100">
          </el-table-column>
          <el-table-column
            prop="name"
            label="名称"
            width="180">
          </el-table-column>
          <el-table-column
            prop="chemical"
            label="化学元素名称"
            width="180">
          </el-table-column>
          <el-table-column
            prop="density"
            label="密度"
            width="180">
          </el-table-column>
          <el-table-column
            prop="hardness"
            label="硬度"
            width="180">
          </el-table-column>
          <el-table-column
            prop="remarks"
            label="材料介绍"
            width="180">
          </el-table-column>
          <el-table-column
            prop="price"
            label="单价"
            width="180">
          </el-table-column>
          <el-table-column
            label="操作"
            width="300">
            <template slot-scope="scope">
              <el-button type="primary" size="mini" @click="updateMaterial(scope.row.id)">编辑</el-button>
              <el-button type="primary" size="mini" @click="look(scope.row.id)">详情</el-button>
              <el-button type="primary"  size="mini" @click="del(scope.row.id)">删除</el-button>
            </template>
          </el-table-column>
        </el-table>
      </el-card>
      <el-pagination
        :current-page="page.pagenum"
        :page-size="page.pagesize"
        :page-sizes="[5, 10, 20, 40]"
        :total="total"
        @current-change="handleCurrentChange"
        @size-change="handleSizeChange"
        layout="total, sizes, prev, pager, next, jumper">
      </el-pagination>
    </div>
</template>

<script>
  export default {
    name: 'MaterialMenu',
    data(){
      return{
        materialList:[],
        material:{},
        page: {
          pagenum: 0,
          pagesize: 5
        },
        total: 0,
      }
    },
    methods:{
      sel:function () {
        this.$http.post("/material/list/"+ this.page.pagenum + "/" + this.page.pagesize,this.material).then((res)=>{
          this.materialList=res.data.result.list;
          this.total = res.data.result.total;
        })
      },
      handleSizeChange: function (val) {
        this.page.pagesize = val;
        this.sel();
      },
      handleCurrentChange: function (val) {
        this.page.pagenum = val;
        this.sel();
      },
      toAdd:function () {
        this.$router.push('/addMaterial');
      },
      del:function (id) {
        this.$http.delete('/material/del/'+id).then((res)=>{
          //alert(id)
          this.materialList=res.data.result;
          this.$message.success("删除成功");
          this.sel()
        })
      },
      look:function (id) {
        this.$router.push({path:'/lookMaterial',query:{id:id}})
      },
      updateMaterial:function (id) {
        this.$router.push({path:'/updateMaterial',query:{id:id}})
      }
    },
    mounted () {
      this.sel();
    }
  }
</script>

<style scoped>
  .text {
    font-size: 14px;
  }

  .item {
    margin-bottom: 18px;
  }

  .clearfix:before,
  .clearfix:after {
    display: table;
    content: "";
  }
  .clearfix:after {
    clear: both
  }

  .box-card {
    width: 1280px;
  }
</style>
